package com.dto.sftp;

import cn.hutool.core.io.FileUtil;
import com.dto.config.IniDTO;
import com.utils.IniReader;
import com.utils.OSUtil;
import com.utils.PathUtil;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Configurable;


import java.io.File;
import java.io.IOException;


@Slf4j(topic = "读取配置文件")

@Configurable
public class ReaderIni {
    //    private static final String filePath = "config.ini";
    public static String filePath;

    //    static {
//        try {
//            filePath = PathUtil.getExePath()+File.separator+"config.ini";
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }
    static {
        if (OSUtil.IsWindowsWin7() == true || OSUtil.IsWindowsWin10() || OSUtil.isWindows() == true) {
            try {
                filePath = PathUtil.getExePath() + File.separator + "config.ini";
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            filePath = PathUtil.getRunPath() + File.separator + "config.ini";
        }


    }

    public ReaderIni() {
        readerIni();

    }


    /**
     * 检查配置信息
     */
    @SneakyThrows
    private static void readerIni() {

        File file = FileUtil.file(ReaderIni.filePath);
        if (file.exists()) {
            IniReader reader = new IniReader(file);
            //assert reader != null;
            IniDTO.setSftp_host(reader.getValue("config", "--sftp.host"));
            IniDTO.setSftp_port(reader.getValue("config", "--sftp.port"));
            IniDTO.setSftp_path(reader.getValue("config", "--sftp.path"));
            IniDTO.setSftp_username(reader.getValue("config", "--sftp.username"));
            IniDTO.setSftp_password(reader.getValue("config", "--sftp.password"));

            IniDTO.setHttpPort(reader.getValue("config", "--api.port"));
            log.info("读取配置文件成功：{}", filePath);
//            System.out.println(IniDTO.myInfo());
        } else {
            log.error("读取配置文件失败" + filePath);
//                doNotify("警告", "配置文件读取失败", "dialog-warning.png");
        }
    }

}
